package ab2.mvc.model
{
	import ab2.mvc.imvc.*;
	import flash.display.Sprite;
	import ab2.mediaserver.*;
	import flash.net.NetConnection;
	import flash.net.SharedObject;
	import mx.controls.Alert;
	import flash.net.*;
	import flash.events.*;
	import mx.utils.StringUtil;
	
	
	public class Model_ extends NetConnection implements IModel
	{
	
	private var _observers:Array;
	private var _newdata:Object;
	private var _vo:Object;
	private var _nc:Model;
	private var _so:SharedObject;
	
	
	NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
	
	 ///////////////////////////////////////////////////////////////
	 //
	 //  Constructor
	 //
	 ///////////////////////////////////////////////////////////////	
	 public function Model_()
	 {
	 	trace ("Model: I collect data");
	 	setobservers();
	 	getVO();
	 	//nc = this;
	 	
	 	// Create the NetConnection and listen for NetStatusEvent and netSecurityError events:
		client = new NetConCalls();
		addEventListener(NetStatusEvent.NET_STATUS, netStatus);
		addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
	 }
	 
	 ///////////////////////////////////////////////////////////////
	 //
	 //  Methods
	 //
	 ///////////////////////////////////////////////////////////////	 
	 public function registerObserver(o:IView):void
	 {
	 	getobservers().push(o);
	 	trace ("Observer count: " + getobservers().length);
	 }
	 
	 public function removeObserver(o:IView):void
	 {
	 	for (var i:uint = 0; i< getobservers().length; i++)
	 	{
	 		if (o == getobservers()[i])
	 		{
	 			getobservers().splice(i,1);
	 		}
	 	}
	 	trace ("Observer count: " + getobservers().length)
	 }
	 
	 public function notifyObservers():void
	 {
	 	for (var i:uint = 0; i< getobservers().length; i++)
	 	{
	 		
	 		getobservers()[i].update(newdata)
	 		
	 	}
	 }
	 
	 
	 ///////////////////////////////////////////////////////////////
	 //
	 //  Unique Methods/ Event Listeners
	 //
	 ///////////////////////////////////////////////////////////////
	 
	 private function netStatus(event:NetStatusEvent):void
	 {
		trace(event.info.code);
		switch(event.info.code)
		{
		case "NetConnection.Connect.Success":
		trace('appConnected()');
		so = SharedObject.getRemote("users", this.uri, false);
        so.client = new SOCalls();
        so.addEventListener(SyncEvent.SYNC, onSoSync);                                  
        so.addEventListener(NetStatusEvent.NET_STATUS, onSOnetStatus);
        so.connect(this);
        so.setProperty("test", "Mustang Rules!")
        setData('appConnected()');
        setData(event.info.code);
		break;
		case "NetConnection.Connect.Closed":
		setData('appClosed()');
		setData(event.info.code);
		break;
		case "NetConnection.Connect.Failed":
		trace('appClosed()');
		break;
		case "NetConnection.Connect.Rejected":
		trace('appClosed()');
		break;
		case "NetConnection.Connect.InvalidApp":
		trace('appClosed()');
		break;
		}
	}

	private function netSecurityError(event:SecurityErrorEvent):void 
	{
		Alert.show("netSecurityError: " + event);
	}
	
	
	private function onSoSync(event:SyncEvent):void 
        {
            trace("sync Event Handler: " + event);
            setData(event)
            trace("usersSync " + event.changeList);
			setData(event.changeList)
        }
        
        
    private function onSOnetStatus(event:NetStatusEvent):void 
        {
            trace("securityErrorHandler: " + event);
        }
        
        
    private function onSOSecurityError(event:SecurityErrorEvent):void 
        {
            trace("securityErrorHandler: " + event);
        }
        
        
	public function doConnect():void
	{
		var fmsURI:String = "rtmp://l2skdrnt98.rtmphost.com/ayo/alpha1";
		connect(fmsURI);
	}

	public function onBWDone ():void
	{
	
	}
	 ///////////////////////////////////////////////////////////////
	 //
	 //  Getter / setters
	 //
	 ///////////////////////////////////////////////////////////////
	 
	 public function setobservers():void
	 {
	 	trace ("Init observers array")
	 	_observers = [];
	 }	


	 public function getobservers():Array 
	 {
	 	return _observers;
	 }
	 
	 
	 public function setVO(vo:Object = null):void
	 {
	 
	 }
	 
	 public function getVO():Object
	 {
	 	return _vo;
	 }
	 
	 public function set nc(c:Model):void
	 {
	 	_nc = c;
	 }
	 
	 public function get nc():Model
	 {
	 	return _nc;
	 }
	 
	 public function set so(s:SharedObject):void
	 {
	 	_so = s;
	 }
	 
	 public function get so():SharedObject
	 {
	 	return _so;
	 }
	 
	 public function setData(o:Object):void
	{
			trace ("setData")
			newdata = o;
			dataChanged();
	}
		
	public function dataChanged():void
		{
			trace ("data changed")
			notifyObservers();
		}
		
	public function set newdata (o:Object):void
		{
			_newdata = o;
		}
		
	public function get newdata ():Object
		{
			return _newdata
		}
	}
}